//
//  AppDelegate.swift
//  MYWeibo
//
//  Created by Magic Yu on 16/4/13.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        setNavBarThemeColor()
        //注册通知
        registerNotification()
        

        
//        test
//        let account = MYUserAccountViewModel.sharedAccountViewModel.loadAccount()
//        print(account)
        
        //创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //设置背景色
        window?.backgroundColor = UIColor.whiteColor()
        //设置根视图
//        window?.rootViewController = MYMainViewController()
//        window?.rootViewController = MYWelcomeViewController()
//        window?.rootViewController = UINavigationController(rootViewController: MYComposeViewController())
//        window?.rootViewController = UINavigationController(rootViewController: MYPictureSelectorController())
        window?.rootViewController = setDefaultViewController()
        //设置为主窗口并可见
        window?.makeKeyAndVisible()
        return true
    }
    
    //注册通知
    private func registerNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootViewController:", name: MYSwitchController, object: nil)
    }
    
    //通知监听的方法
    @objc private func changeRootViewController(n: NSNotification) {
    
        if n.object != nil {
            //从欢迎页面发来的通知
            window?.rootViewController = MYMainViewController()
        } else {
            //从授权页面发来的通知
            window?.rootViewController = MYWelcomeViewController()
        }
        
    }
    
    //根据用户是否登录 设置根视图
    private func setDefaultViewController() -> UIViewController {
    
        return MYUserAccountViewModel.sharedAccountViewModel.userLogin ? MYWelcomeViewController() : MYMainViewController()
        
    }

    private func setNavBarThemeColor() {
        //设置全局颜色
        //全局颜色的设置 必须在控件实例化之前
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    
    //移除通知
    deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}








